package com.dji.activationDemo.messaging;

//import static com.dji.activationDemo.MainActivity.mSendVirtualStickDataTimer;

import com.dji.activationDemo.Constant;
import com.dji.activationDemo.uitls.ModuleVerificationUtil;
import com.dji.activationDemo.uitls.RandomNum;
import com.dji.activationDemo.websocket.WsManager;

import java.util.Timer;
import java.util.TimerTask;

import dji.common.error.DJIError;
import dji.common.flightcontroller.virtualstick.FlightControlData;
import dji.common.util.CommonCallbacks;
import dji.sdk.flightcontroller.FlightController;
import dji.sdk.remotecontroller.S;

public class Simulator {
    FlightController flightController = ModuleVerificationUtil.getFlightController();
    RandomNum randomNum = new RandomNum();
    private SendVirtualStickDataTask mSendVirtualStickDataTask;
    private float mPitch;
    private float mRoll;
    private float mYaw;
    private float mThrottle;
    private Timer mSendVirtualStickDataTimer;

    private long screenJoystickLeftTime = System.currentTimeMillis();
    private long screenJoystickRightTime = System.currentTimeMillis();
    private Thread screenJoystickThread = null;

    public void startTakeoff(String sign, String sendName) {
        System.out.println("起飞");
        flightController.startTakeoff(new CommonCallbacks.CompletionCallback() {
            @Override
            public void onResult(DJIError djiError) {
                if (djiError == null) {
                    WsManager.ws.sendText("received*" + sign);
                }
            }
        });
    }

    public void startLanding(String sign) {
        System.out.println("降落");
        flightController.startLanding(new CommonCallbacks.CompletionCallback() {
            @Override
            public void onResult(DJIError djiError) {
                if (djiError == null) {
                    WsManager.ws.sendText("received*" + sign);
                }
            }
        });
    }

    public void confirmLanding(String sign) {
        System.out.println("着陆");
        flightController.confirmLanding(new CommonCallbacks.CompletionCallback() {
            @Override
            public void onResult(DJIError djiError) {
                if (djiError == null) {
                    WsManager.ws.sendText("received*" + sign);
                }

            }
        });
    }

    public void setVirtualStickModeEnabled(boolean state, String sign) {
        if (flightController != null) {

            flightController.setVirtualStickModeEnabled(state, new CommonCallbacks.CompletionCallback() {
                @Override
                public void onResult(DJIError djiError) {
                    if (djiError != null) {
                        System.out.println(djiError.getDescription());
                    } else {
                        System.out.println("虚拟棒：" + state);
                        if (state) {
                            Constant.screenJoystickExecution = true;
                            if (screenJoystickThread == null) {
                                screenJoystickThread();
                            }
                            screenJoystickThread.start();
                        } else {
                            Constant.screenJoystickExecution = false;
                            screenJoystickThread.interrupt();
                        }

                        WsManager.ws.sendText("received*" + sign);
                    }
                }
            });

        }
    }

    //左虚拟棒控制上升和旋转
    public void ScreenJoystickLeft(String paramString, String sign) {
        System.out.println("进入左虚拟棒方法");
        String[] aa = paramString.split(",");
        float pX, pY;
        pX = Float.parseFloat(aa[1]);
        pY = Float.parseFloat(aa[2]);

        if (Math.abs(pX) < 0.02) {
            pX = 0;
        }

        if (Math.abs(pY) < 0.02) {
            pY = 0;
        }
        float verticalJoyControlMaxSpeed = 2;
        float yawJoyControlMaxSpeed = 30;

        mYaw = (float) (yawJoyControlMaxSpeed * pX);
        mThrottle = (float) (verticalJoyControlMaxSpeed * pY);
        screenJoystickLeftTime = System.currentTimeMillis();
        WsManager.ws.sendText("received*" + sign);
        if (null == mSendVirtualStickDataTimer) {
            mSendVirtualStickDataTask = new SendVirtualStickDataTask();
            mSendVirtualStickDataTimer = new Timer();
            mSendVirtualStickDataTimer.schedule(mSendVirtualStickDataTask, 0, 200);
        }
    }

    //右虚拟棒控制前进和左右
    public void ScreenJoystickRight(String paramString, String sign) {
        System.out.println("ScreenJoystickRight:" + Thread.currentThread().getName());

        System.out.println("进入右虚拟棒方法");
        String[] aa = paramString.split(",");
        float pX, pY;
        pX = Float.parseFloat(aa[1]);
        pY = Float.parseFloat(aa[2]);

        if (Math.abs(pX) < 0.02) {
            pX = 0;
        }

        if (Math.abs(pY) < 0.02) {
            pY = 0;
        }

        float pitchJoyControlMaxSpeed = 10;
        float rollJoyControlMaxSpeed = 10;

        mPitch = (float) (pitchJoyControlMaxSpeed * pX);
        mRoll = (float) (rollJoyControlMaxSpeed * pY);
        screenJoystickRightTime = System.currentTimeMillis();
        WsManager.ws.sendText("received*" + sign);
        if (null == mSendVirtualStickDataTimer) {
            mSendVirtualStickDataTask = new SendVirtualStickDataTask();
            mSendVirtualStickDataTimer = new Timer();
            mSendVirtualStickDataTimer.schedule(mSendVirtualStickDataTask, 0, 200);
        }

    }

    class SendVirtualStickDataTask extends TimerTask {
        @Override
        public void run() {
            if (flightController != null) {
                System.out.println("mPitch:" + mPitch + ",mRoll:" + mRoll + ",mYaw:" + mYaw + ",mThrottle:" + mThrottle);
                flightController.sendVirtualStickFlightControlData(
                        new FlightControlData(
                                mPitch, mRoll, mYaw, mThrottle
                        ), djiError -> {

                        }
                );
            }
        }
    }

    private void screenJoystickThread() {
        screenJoystickThread = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (this) {
                    while (Constant.screenJoystickExecution) {
                        if (Thread.currentThread().isInterrupted()) {
                            break;
                        }
                        try {
                            long timeOutLeft = System.currentTimeMillis();
                            long timeOutRight = System.currentTimeMillis();
                            if ((timeOutLeft - screenJoystickLeftTime) > 1000) {
                                mYaw = 0;
                                mThrottle = 0;
                            }
                            if ((timeOutRight - screenJoystickRightTime) > 1000) {
                                mPitch = 0;
                                mRoll = 0;
                            }
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }


}

